package com.zb.controller.download;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zb.common.annotation.Permission;
import com.zb.common.annotation.WriteLog;
import com.zb.common.exception.BusinessException;
import com.zb.common.po.enums.ResponseCode;
import com.zb.common.po.model.DownLoad;
import com.zb.common.po.query.DownLoadQuery;
import com.zb.common.po.vo.AjaxResponse;
import com.zb.common.utils.PaginationResult;
import com.zb.common.utils.SessionUserUtil;
import com.zb.controller.settings.MenuController;
import com.zb.service.DownloadService;

@Controller
@RequestMapping(value = "/download")
public class DownLoadController {

    Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Resource
    private DownloadService downloadService;

    @Permission(code = "download:down")
    @RequestMapping(value = "/download.do")
    public ModelAndView download(HttpSession session) {
        ModelAndView mv = new ModelAndView("page/download/download");
        DownLoad donwload = this.downloadService.findDownloadByUserId(SessionUserUtil.getSessionUser(session)
                        .getUserId());
        int count = donwload == null ? 0 : donwload.getDownloadCount();
        mv.addObject("count", count);
        return mv;
    }

    @Permission(code = "download:down")
    @RequestMapping(value = "/downloadFile.do", method = RequestMethod.GET)
    @WriteLog(opObject = "源码下载", opDescription = "源码下载")
    public ModelAndView downloadFile(HttpSession session,
            HttpServletRequest request, HttpServletResponse response,
            Integer type) {
        ModelAndView mv = new ModelAndView("page/download/downloaderror");
        try {
            downloadService.downloadFile(SessionUserUtil
                    .getSessionUser(session).getUserId(), request, response,
                    type);
        } catch (BusinessException e) {
            mv.addObject("error", e.getMessage());
        } catch (Exception e) {
            mv.addObject("error", "下载异常请联系多多洛");
        }
        return mv;
    }

    @Permission(code = "download:down")
    @ResponseBody
    @RequestMapping(value = "/checkDownLoad.do")
    public AjaxResponse<?> checkDownLoad(HttpSession session) {
        AjaxResponse<?> response = new AjaxResponse<Object>();
        try {
            DownLoad donwload = this.downloadService
                    .findDownloadByUserId(SessionUserUtil.getSessionUser(
                            session).getUserId());
            if (donwload.getDownloadCount() == 0) {
                response.setResponseCode(ResponseCode.BUSINESSERROR);
                response.setErrorMsg("你的下载次数已经用完");
            }
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error("校验用户下载状态失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("校验用户下载状态失败");
        }
        return response;
    }

    @Permission(code = "download:show_info")
    @RequestMapping(value = "/downLoadInfoList.do")
    public ModelAndView findDownLoadInfoList(HttpSession session)
            throws ServletException, IOException {
        ModelAndView mv = new ModelAndView("page/download/download_list");
        return mv;
    }

    @Permission(code = "download:show_info")
    @ResponseBody
    @RequestMapping(value = "/loadDownLoadInfoList.do")
    public AjaxResponse<PaginationResult<DownLoad>> loadDownLoadInfoList(
            DownLoadQuery query) {
        AjaxResponse<PaginationResult<DownLoad>> response = new AjaxResponse<PaginationResult<DownLoad>>();
        try {
            PaginationResult<DownLoad> result = this.downloadService
                    .findDownLoadList(query);
            response.setData(result);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error("查询下载信息失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("查询下载信息失败");
        }
        return response;
    }

    @Permission(code = "download:down-video")
    @RequestMapping(value = "/videodownload.do")
    public ModelAndView videodownload(HttpSession session)
            throws ServletException, IOException {
        ModelAndView mv = new ModelAndView("page/download/videodownload");
        return mv;
    }

    @Permission(code = "download:down-code")
    @RequestMapping(value = "/getCode.do")
    public ModelAndView getCode(HttpSession session) {
        ModelAndView mv = new ModelAndView("page/download/getCode");
        return mv;
    }
}
